//=============所有使用到geojson文件或字段的方法==========
import global from "../../core/global.js";
import { convertColorFormat } from "../../core/common.js";
import { showLayerByLayer, cleanTestLayer } from "./handle-layer.js";


let geoLineLayerList = [];
/**
 * @description 画点 geojson
 * @param { object } opt
 * @return GeoJsonPolylineLayer
 */
export function addGeoJsonLabelLayer(opt, cb) {
  Object.assign(opt, opt.geojsonInfo || {})
  opt.altitudetype = "RelativeToTerrain"
  if (!opt.labelField) opt.labelField = 'defaultName'
  // 已经加载则不重复加载 改用显隐
  let needReload = showLayerByLayer(opt, 'enabled')
  if (!needReload) {
    cb && cb()
    return
  }
  let GeoJsonLabelLayer = new SSmap.GeoJsonLabelLayer()
  GeoJsonLabelLayer.addString(opt.geoStr)
  GeoJsonLabelLayer.setNameField(opt.labelField)
  if (opt.backgroundColor && opt.labelField != 'defaultName') {
    const color = convertColorFormat(opt.backgroundColor)
    GeoJsonLabelLayer.background = SSmap.Color.fromRgb(color.r, color.g, color.b, color.a)
  }
  opt.fontColor = opt.fontColor || '#ffffff'
  if (opt.fontColor && opt.labelField != 'defaultName') {
    const color = convertColorFormat(opt.fontColor)
    GeoJsonLabelLayer.fontColor = SSmap.Color.fromRgb(color.r, color.g, color.b, color.a)
  }
  opt.strokeColor = opt.strokeColor || '#00C8FF'
  if (opt.strokeColor && opt.labelField != 'defaultName') {
    const color = convertColorFormat(opt.strokeColor)
    GeoJsonLabelLayer.strokeColor = SSmap.Color.fromRgb(color.r, color.g, color.b, color.a)
  }
  GeoJsonLabelLayer.scale = 1.2
  GeoJsonLabelLayer.imageWidth = opt.iconSize || 50
  GeoJsonLabelLayer.imageheight = opt.iconSize || 50
  GeoJsonLabelLayer.imageHeight = opt.iconSize || 50
  GeoJsonLabelLayer.glyphCenter = 0.7
  GeoJsonLabelLayer.outlineCenter = 0.75
  // GeoJsonLabelLayer.mix = true
  // GeoJsonLabelLayer.vertical = SSmap.Vertical.CENTER_V
  // GeoJsonLabelLayer.textVertical = SSmap.Vertical.TOP
  let iconUrl
  if (!opt.iconUrl) iconUrl = 'images/point-icon/2-橙-未选中@2x.png'
  else iconUrl = opt.iconUrl
  if (opt.iconColor) iconUrl = iconUrl.replace(/(-)([^-]+)(-未选中)/, `$1${opt.iconColor}$3`)
  GeoJsonLabelLayer.url = window.baseURL + '/' + iconUrl
  GeoJsonLabelLayer.usedProperties = true //使用属性(会读取geojson文件中的属性, 用于拾取属性)
  if (opt.altitudetype) {
    GeoJsonLabelLayer.setAltitudeMethod(SSmap.AltitudeMethod[opt.altitudetype])
  }
  if (opt.altitude && (opt.altitudetype == "Absolute" || opt.altitudetype == "RelativeToTerrain")) {
    GeoJsonLabelLayer.setAltitude(opt.altitude) //5.0
  }
  GeoJsonLabelLayer.setDistanceDisplayCondition(SSmap.Vector2.create(1, 5.0e100))
  GeoJsonLabelLayer.setScaleByDistance(
    SSmap.Vector4.create(0, 0.8, 1.0e8, 0.9)
  ); //缩放比
  GeoJsonLabelLayer.setTranslucencyByDistance(
    SSmap.Vector4.create(1, 0.9, 1.0e8, 0.8)
  ); //缩放控制透明度
  if (opt.id && opt.id != 'temp_id_12138') {
    GeoJsonLabelLayer.tag = opt.id
    GeoJsonLabelLayer.dataType = opt.dataType
    global.layerList.add(GeoJsonLabelLayer, opt.id)
  } else {
    GeoJsonLabelLayer.tag = 'temp_id_12138'
    cleanTestLayer()
    global.testLayer = GeoJsonLabelLayer
  }
  GeoJsonLabelLayer.create()
  cb && cb()
  return GeoJsonLabelLayer
}
/**
 * @description 画线 geojson
 * @param { object } opt 画面参数：线宽，透明度，节点数组，颜色，名称，Vector3
 * @return GeoJsonPolylineLayer
 */
export function addGeoJsonPolylineLayer(opt, cb) {
  Object.assign(opt, opt.geojsonInfo || {})
  opt.altitudetype = opt.altitudetype || "RelativeToTerrain"
  let geoPolylineLayer = new SSmap.GeoJsonPolylineLayer()
  if (opt.url) {
    geoPolylineLayer[opt.url.split('.')[-1] == 'geojson' ? 'geoJson' : 'shapefile'] = opt.showUrl || opt.url
  }
  if (opt.geoStr) {
    geoPolylineLayer.addString(opt.geoStr)
  }
  if (opt.lineColor) {
    const color = convertColorFormat(opt.lineColor)
    geoPolylineLayer.color = SSmap.Color.fromRgb(color.r, color.g, color.b, color.a)
    opt.alpha = color.a / 255
  }
  geoPolylineLayer.colorField = opt.colorField || 'colorField'
  geoPolylineLayer.widthField = opt.widthField || 'widthField'
  geoPolylineLayer.glow = opt.glow == undefined ? false : opt.glow;
  if (opt.glowPower) {
    geoPolylineLayer.glow = true
    geoPolylineLayer.glowPower = opt.glowPower
  }
  geoPolylineLayer.alpha = opt.alpha == undefined ? 0.99 : opt.alpha == 1 ? 0.99 : opt.alpha;
  geoPolylineLayer.dash = opt.dash == undefined ? true : opt.dash;
  geoPolylineLayer.width = opt.lineWidth == undefined ? 1 : opt.lineWidth;
  geoPolylineLayer.usedProperties = true //使用属性(会读取geojson文件中的属性, 用于拾取属性)
  geoPolylineLayer.animationRun = opt.animationRun == undefined ? false : opt.animationRun;//动画否
  geoPolylineLayer.animationTimer = opt.animationTimer == undefined ? 5000 : opt.animationTimer;//动画周期
  geoPolylineLayer.depthTest = opt.depthTest == undefined ? true : opt.depthTest;
  if (opt.altitudetype) {
    geoPolylineLayer.setAltitudeMethod(SSmap.AltitudeMethod[opt.altitudetype])
  }
  if (opt.altitude && (opt.altitudetype == "Absolute" || opt.altitudetype == "RelativeToTerrain")) {
    geoPolylineLayer.setAltitude(opt.altitude) //5.0
  }
  geoPolylineLayer.setDistanceDisplayCondition(SSmap.Vector2.create(0, 5.0e100))
  if (opt.id && opt.id != 'temp_id_12138') {
    geoPolylineLayer.tag = opt.id
    geoPolylineLayer.dataType = opt.dataType
    global.layerList.add(geoPolylineLayer, opt.id)
  } else {
    geoPolylineLayer.tag = 'temp_id_12138'
    cleanTestLayer()
    global.testLayer = geoPolylineLayer
  }
  geoPolylineLayer.create()
  geoPolylineLayer.readyPromise.then((res)=>{
    let cameraController = GlobalViewer.scene.mainCamera.cameraController()
    cameraController.flyToRectangle(geoPolylineLayer.extent(), 2, 0, -90, 0)
  })
  geoLineLayerList.push({
    geoPolylineLayer,
    id: opt.id,
  });
      cb && cb(geoPolylineLayer)
  
  return geoPolylineLayer
}

export function delGeoJsonPolylineLayer(id) {
  if (geoLineLayerList.length > 0) {
    geoLineLayerList.forEach((item) => {
      if (item.id == id) {
        item.geoPolylineLayer.delete();
      }
    });
    geoLineLayerList = geoLineLayerList.filter((item) => {
      return item.id != id;
    });
  }
}
/**
 * @description 画面 geojson
 * @param { object } opt
 * @return GeoJsonPolygonLayer
 */
export function addGeoJsonPolygonLayer(opt, cb) {
  Object.assign(opt, opt.geojsonInfo || {})
  opt.altitudetype = "RelativeToTerrain"
  // 已经加载则不重复加载 改用显隐
  let needReload = showLayerByLayer(opt, 'delete')
  if (!needReload) {
    cb && cb()
    return
  }
  let geoJsonPolygonLayer = new SSmap.GeoJsonPolygonLayer()
  if (opt.url) {
    geoJsonPolygonLayer[opt.url.split('.')[-1] == 'geojson' ? 'geoJson' : 'shapefile'] = opt.showUrl || opt.url
  }
  if (opt.geoStr) {
    geoJsonPolygonLayer.addString(opt.geoStr)
  }
  geoJsonPolygonLayer.lineColor = SSmap.Color.fromRgb(0, 0, 0, 255)
  if (opt.lineColor) {
    const color = convertColorFormat(opt.lineColor)
    geoJsonPolygonLayer.lineColor = SSmap.Color.fromRgb(color.r, color.g, color.b, color.a)
  }
  if (opt.fillColor) {
    const color = convertColorFormat(opt.fillColor)
    geoJsonPolygonLayer.fillColor = SSmap.Color.fromRgb(color.r, color.g, color.b, color.a)
    opt.fillAlpha = color.a / 255
  }
  geoJsonPolygonLayer.usedProperties = true //使用属性(会读取geojson文件中的属性, 用于拾取属性)
  geoJsonPolygonLayer.colorField = opt.colorField || 'colorField' //面线框颜色
  geoJsonPolygonLayer.fillColorField = opt.fillColorField || 'fillColorField' //面填充颜色
  geoJsonPolygonLayer.entityType = opt.entityType || SSmap.EntityType.PLANE;
  geoJsonPolygonLayer.lineAlpha = opt.fillAlpha == undefined ? 0.99 : opt.fillAlpha == 1 ? 0.99 : opt.fillAlpha;//线的透明度设置（注意填充透明度不能为1，不然视角超出一定范围会消失）
  geoJsonPolygonLayer.fillAlpha = opt.fillAlpha == undefined ? 0.99 : opt.fillAlpha == 1 ? 0.99 : opt.fillAlpha;//填充面的透明度设置（注意填充透明度不能为1，不然视角超出一定范围会消失）
  geoJsonPolygonLayer.lineWidth = opt.lineWidth == undefined ? 1 : opt.lineWidth;
  geoJsonPolygonLayer.depthTest = opt.depthTest == undefined ? false : opt.depthTest;
  geoJsonPolygonLayer.depthTestAltitude = opt.depthTestAltitude == undefined ? 5000 : opt.depthTestAltitude;
  geoJsonPolygonLayer.entityType = opt.entityType || SSmap.EntityType.PLANE;
  if (opt.altitudetype) {
    geoJsonPolygonLayer.setAltitudeMethod(SSmap.AltitudeMethod[opt.altitudetype])
  }
  if (opt.altitude && (opt.altitudetype == "Absolute" || opt.altitudetype == "RelativeToTerrain")) {
    geoJsonPolygonLayer.setAltitude(opt.altitude) //5.0
  }
  geoJsonPolygonLayer.setDistanceDisplayCondition(SSmap.Vector2.create(0, 5.0e100))
  if (opt.id && opt.id != 'temp_id_12138') {
    geoJsonPolygonLayer.tag = opt.id
    geoJsonPolygonLayer.dataType = opt.dataType
    global.layerList.add(geoJsonPolygonLayer, opt.id)
  } else {
    geoJsonPolygonLayer.tag = 'temp_id_12138'
    cleanTestLayer()
    global.testLayer = geoJsonPolygonLayer
  }
  geoJsonPolygonLayer.create()
  cb && cb()
  return geoJsonPolygonLayer
}
